# Program:  process_training.py
#
# Author:  Patrick Raycroft
#
# This file will create a file that contains
# the vectors for the training data
#
# Updated:  April 21, 2013

import subprocess as sp
import shlex
import re

stats = dict()

def read_team_stats(lowerYear, upperYear):
	"""
	This function reads in .csv files containing team stats
	from lowerYear to upperYear (inclusive)
	"""
	year = lowerYear
	while (year <= upperYear):
		yearStr = str(year)
		stats[yearStr] = dict()
		filename = 'teamstats_' + yearStr + '.csv'
		f = open(filename, 'r')
		lines = f.readlines()
		lines.pop(0)
		for line in lines:
			fields = line.split(',')
			team = fields.pop(0)
			fields.pop(0)
			stats[yearStr][team] = fields
		year += 1

def write_training_file(inFile, outFile):
	"""
	This function creates a file of usable training data
	named 'filename'
	Format:
		GameID
		Team A Stats
		Team B Stats
		Difference of Stats
		Class (1 if Team A wins, -1 if Team B wins)
	"""
	# Remove any old file and create a blank output file
	cmd = 'rm -f ' + outFile
	sp.Popen(shlex.split(cmd))
	cmd = 'touch ' + outFile
	sp.Popen(shlex.split(cmd))

	ifile = open(inFile, 'r')
	ofile = open(outFile, 'a')
	lines = ifile.readlines()
	gameID = 1
	for line in lines:
		fields = line.split(',')
		year = fields[0]
		hold = fields[1]
		winner = re.sub(r'[^\w-]','',re.sub(r' ','-', hold.strip().lower()))
		#winner = fields[1]
		hold = fields[2]
		loser = re.sub(r'[^\w-]','',re.sub(r' ','-', hold.strip().lower())) 
		#loser = fields[2]
		winnerStats = stats[year][winner]
		loserStats = stats[year][loser]
		diff = [] # Winner - Loser
		revDiff = [] # Loser - Winner
		for s in range(len(winnerStats) - 1):
			diff.append(str(float(winnerStats[s]) - float(loserStats[s])))
			revDiff.append(str(float(loserStats[s]) - float(winnerStats[s])))
		
		# Create entry for Winner - Loser Statline
		ofile.write('G:' + str(gameID))
		ofile.write('\n')
		ofile.write('C:1\n')
		ofile.write('T1:')
		for w in range(len(winnerStats) - 1):
			ofile.write(winnerStats[w])
			if (w != len(winnerStats) - 2): ofile.write(',')
		ofile.write('\n')
		ofile.write('T2:')
		for l in range(len(loserStats) - 1):
			ofile.write(loserStats[l])
			if (l != len(loserStats) - 2): ofile.write(',')
		ofile.write('\n')
		ofile.write('D:')
		for d in range(len(diff) - 1):
			ofile.write(diff[d])
			if (d != len(diff) - 2): ofile.write(',')
		ofile.write('\n')
			
		# Create entry for Loser - Winner Statline
		ofile.write('G:' + str(gameID))
		ofile.write('\n')
		ofile.write('C:-1\n')
		ofile.write('T1:')
		for l in range(len(loserStats) - 1):
			ofile.write(loserStats[l])
			if (l != len(loserStats) - 2): ofile.write(',')
		ofile.write('\n')
		ofile.write('T2:')
		for w in range(len(winnerStats) - 1):
			ofile.write(winnerStats[w])
			if (w != len(winnerStats) - 2): ofile.write(',')
		ofile.write('\n')
		ofile.write('D:')
		for r in range(len(revDiff) - 1):
			ofile.write(revDiff[r])
			if (r != len(revDiff) - 2): ofile.write(',')
		ofile.write('\n')
		
		# Increment game ID
		gameID += 1

	ifile.close()
	ofile.close()


if __name__=="__main__":
	read_team_stats(2008, 2012)
	write_training_file('training_raw.csv', 'training_processed.csv')
